﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using DataAccess;

namespace tracnghiemonline2
{
    public partial class masterpagecuatui : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btn_exit_ServerClick(object sender, EventArgs e)
        {
            Session["user"] = null;
            Session["level"] = null;
            Session["user_id"] = null;
            Response.Cookies["user"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("~/Default.aspx");
        }

        protected void btn_Login(object sender, EventArgs e)
        {
            string username = txt_login_username.Value;
            string password = txt_login_password.Value;

            var get_user = new TblUserController().FetchAll().Where(x => x.UserName.ToLower() == username.ToLower() && x.UserPass == password);
            if (get_user.ToList().Count != 0)
            {
                Session["user"] = get_user.Single().UserName;
                Session["user_id"] = get_user.Single().UserId;
                Session["level"] = get_user.Single().UserLevel;
                HttpCookie ck = new HttpCookie("user");
                ck.Value = get_user.Single().UserId.ToString();
                Response.Cookies.Add(ck);
                Page.Response.Redirect("~/bai-hoc", true);
            }
            else
            {
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "javascript:alert('Sai tên đăng nhập hoặc mật khẩu!')", true);
                login_msg.InnerText = "Sai tài khoản hoặc mật khẩu";
                login_msg_area.Visible = true;
            }
            //txt_login_username.Value = DateTime.Now.ToString();
        }
    }
}